package com.sx.micowell.module.more

import android.view.View
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.mvvm.base.ContainerActivity
import com.example.mvvm.base.action.TitleBarAction
import com.example.mvvm.base.data.DataBindingConfig
import com.gyf.immersionbar.ImmersionBar
import com.example.mvvm.bar.TitleBar
import com.miloyu.modelnet.response.getResId
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.FragMoreCourseBinding
import com.sx.micowell.framework.app.MyFragment
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.model.RecommendCourseModel
import com.sx.micowell.module.course.CourseTabEntity
import com.sx.widget.view.flyco.tablayout.listener.OnTabSelectListener
import kotlinx.android.synthetic.main.frag_recommend_course.*
import java.util.*
import kotlin.streams.toList

/**
 * @Author: YY
 * @Time: 2021/9/6
 * @Describe 更多课程
 */
@Route(path = RoutePath.MORE_COURSE)
class MoreCourseFragment : MyFragment<FragMoreCourseBinding, ContainerActivity>(), TitleBarAction {

    lateinit var viewModel: RecommendCourseModel

    override fun initViewModel() {
        viewModel = getFragmentScopeViewModel(RecommendCourseModel::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig =
        DataBindingConfig(R.layout.frag_more_course, BR.viewModel, viewModel)
            .addBindingParam(BR.manager, childFragmentManager)

    override fun initView() {
        // 设置状态栏字体颜色
        ImmersionBar.with(this).statusBarDarkFont(true)
            .fitsSystemWindows(true).init()
        binding.title.setOnTitleBarListener(this)
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.courseTypeList.observe(this, {
            var _on = "_on"
            var _off = "_off"
            var drawable = "drawable"
            var arrayList = it.stream().map {
                CourseTabEntity(
                    context?.getResId(drawable, it.dictValue.lowercase(Locale.getDefault()) + _on) as Int,
                    context?.getResId(drawable, it.dictValue.lowercase(Locale.getDefault()) + _off) as Int,
                    it.dictLabel
                )
            }.toList()
            course_tab.setTabData(arrayList)
            viewModel.initMorePage()
        })
    }

    override fun initData() {
        viewModel.reqCourseType()
        course_tab.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                course_vp.currentItem = position
            }

            override fun onTabReselect(position: Int) {

            }
        })
        course_vp.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                course_tab.currentTab = position;
            }

            override fun onPageScrollStateChanged(state: Int) {
            }

        })
    }

    override fun getTitleBar(): TitleBar? {
        return binding.title
    }

    override fun onLeftClick(v: View?) {
        myActivity.finish()
    }
}